package com.sea.base.adapter.utils

import android.view.View
import android.view.ViewGroup
import androidx.core.view.get

object ViewGroupWrapUtils {
    /**
     * 获得child的宽高
     */
    fun getChildWidthHeight(vg: ViewGroup, listener: (width: Int, height: Int) -> Unit) {
        vg.post {
            if (vg.childCount > 0 && (vg[0].width > 0 || vg[0].height > 0)) {
                listener.invoke(vg[0].width, vg[0].height)
            } else {
                vg.addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
                    override fun onLayoutChange(view: View, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
                        if (vg.childCount > 0) {
                            val childAt = vg[0]
                            if (childAt.width > 0 || childAt.height > 0) {
                                vg.removeOnLayoutChangeListener(this)
                                listener.invoke(childAt.width, childAt.height)
                            }
                        }
                    }
                })
            }
        }
    }
}